using UnityEngine;
using UnityEngine.UI;

public class EE_UI_Building : MonoBehaviour
{
    [SerializeField] private EE_Data.BuildingID id = EE_Data.BuildingID.townhall;
    
    private Button button = null;

    private void Awake()
    {
        button = GetComponent<Button>();
    }

    private void Start()
    {
        button.onClick.AddListener(Clicked);
    }
    
    private void Clicked()
    {
        // 获取预制体
        EE_Building_Controller prefab = EE_UI_Main.Instance.GetBuildingPrefab(id);
        if (prefab)
        {
            EE_UI_Shop.Instance.SetStatus(false);
            EE_UI_Main.Instance.SetStatus(true);
        
            // building的网格坐标位置初始化
            Transform gridTransform = EE_UI_Main.Instance.grid.transform;
            Vector3 gridPosition = gridTransform.InverseTransformPoint(Vector3.zero);
            int x = Mathf.FloorToInt(gridPosition.x);
            int z = Mathf.FloorToInt(gridPosition.z);
            // Debug.Log($"网格坐标：{x}, 0, {z}");
            // 网格坐标转为世界坐标
            Vector3 position = gridTransform.TransformPoint(new Vector3(x, 0, z));
            // Debug.Log($"世界坐标：{position}");
            // 生成建筑预制体
            EE_Building_Controller building = Instantiate(prefab, position, Quaternion.identity);
            building.x = x;
            building.y = z;
            building.CurrentX = x;
            building.CurrentY = z;
            
            // building.PlaceOnGrid(x, z);
            building.baseArea.gameObject.SetActive(true);
            
            EE_Building_Controller.Instance = building;
            EE_MainCamera_Controller.Instance.isPlacingBuilding = true;
            // 点击建造后，打开确认建造按钮V和X
            EE_UI_Build.Instance.SetStatus(true);
        }
    }
}